"""
访问京东商城网址，选择多个商品放入购物车后查看自己的购物车，显示效果如下 。
请使用Python爬取京东商城网址购物车中的所有商品信息: 
如URL地址：https://cart.jd.com/cart.action 

抓包后网址：
https://rsc.jd.com/rsc/cartCompare?callback=jQuery1003352&
cartInfo=12398725_1554804284395%2C11993134_1554804276485%
2C100003936976_1554804257046%2C100001550349_1554804251269%
2C100003433872_1554804245488%2C100000287141_1554804238155&
area=1%2C72%2C2799%2C0&_=1554805164840
"""

import requests
from pyquery import PyQuery as pq
import csv


def getPage(url):
    """
    获取网页完整源代码
    :param url: 
    :return: 
    """
    headers = {
        'accept': 'text / html, application / xhtml + xml, application / xml;q = 0.9, image / webp, image / apng, * / *;q = 0.8',
        # 'accept - encoding': 'gzip, deflate, br',
        'accept - language': 'zh - CN, zh;q = 0.9, en;q = 0.8',
        'cache - control': 'max - age = 0',
        'cookie': '__jdu = 523271859;shshshfpa = b8cfb73b - 6def -08da - 1cd0 - 8770776577a2 - 1550156907;shshshfpb = rLMw07Bis8VXYneqx % 20kXVVA % 3D % 3D;PCSYCityID = 1659;ipLoc - djd = 1 - 72 - 2799 - 0;mt_xid = V2_52007VwcRUF5eUlMbQSlbUGUAGlUOC05TGBsfQAAwVBNODllaDwNAGQ9SZAAaWw9ZUlMvShhcDXsCF05cX0NaHUIcWg5kByJQbVhiUxdNHFsGbgIRVV9cV1IeTRBbAFcGFQ % 3D % 3D;unpl = V2_ZzNtbUYFFBZ0W0RUcxsIVWJQQFVLAhBGcQBBBnIeWAFkBkIOclRCFX0UR1FnGFQUZwUZWUpcQhBFCEdkeBBVAWMDE1VGZxBFLV0CFSNGF1wjU00zQwBBQHcJFF0uSgwDYgcaDhFTQEJ2XBVQL0oMDDdRFAhyZ0AVRQhHZH8aXgZhAxtZSmdzEkU4dlJ4GF4EYDMTbUNnAUEpAERWcx9bSGMAEF5EV0oRfThHZHg % 3d;unick = % E8 % 90 % 9D % E5 % 8D % 9C1222;pin = % E8 % 90 % 9D % E5 % 8D % 9C1222;_tp = LyM5ojiWa % 2BrJoN52D6d9IHivihYK7KLWOp5QXE912RM % 3D;__jdv = 122270672 | baidu - pinzhuan | t_288551095_baidupinzhuan | cpc | 0f3d30c8dba7459bb52f2eb5eba8ac7d_0_5cf20b3193ea4bc98dbb596c865524ab | 1554804220806;_gcl_au = 1.1.242078635.1554804231;cart - main = xx;ipLocation = % u5317 % u4EAC;areaId = 1;cd = 0;shshshfp = c3e83cd0dd67baad0439ad463e482cdf;user - key = 29a1891e - fa17 - 45b4 - 8255 - 2bd1405dfaa5;pinId = 3t12Vngh5ylQGl9BPCfw5w;_pst = % E8 % 90 % 9D % E5 % 8D % 9C1222;__jda = 122270672.523271859.1550156904.1554902588.1554988171.12;__jdc = 122270672;wlfstk_smdl = 1id4cagja8xq77oporaoqf3pn9z52hy3;TrackID = 15omxKbMR - R7Hl5_sW3xi29aSLR2wLPhZ - 0NwtefzegYOPtzWXuv - udVttiNr8qKTi8k0zj7ySsDRFGDia4hvDFL4d_q0m1NcJDey0ffMaQc;thor = 33DDF8C5C94CCA8D695C578B76C1691C10897B2F0B199284931C49A2E7FD5FECE8578CBA20A80041AA3810DBBF95AFDE1D6EE505C4EED0E152ED3A08283F6D37AC6AD8DE59F2CF1A3A6DC8B8C9042CE90E6ED3BDF85B38EE2A7C83D9A0AAF245E985145891F71FFA1A1644EDF132E2719165DC57C3830A5CA32F5E264DF8BC13;ceshi3.com = 000;cn = 6;3AB9D23F7A4B3C9B = LFWXE6YW2CFRSM7POI3F542KJ3RUCV55LJMQT2B3Y4GCGA3TF4UYCPSONVGYSJSHEOPDGYSYO5UYREH3E2FOMYPW24',
        'referer': 'https: // cart.jd.com / cart.action',
        'upgrade - insecure - requests': '1',
        'user - agent': 'Mozilla / 5.0(WindowsNT10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 70.0.3538.110Safari / 537.36',
    }
    try:
        res = requests.get(url, headers=headers)
        if res.status_code == 200:
            return res.text
        else:
            print('存在异常')
            return None
    except Exception as error:
        print(error)
        return None  # 若存在异常，则打印异常并返回空值，可以尝试故意输错网址


def parsePage(content):
    """
    解析所在网页的网页源代码
    :param content: 
    :return: 
    """
    doc = pq(content)
    items = doc('div.item-item')
    #print(len(items))  # 检查是否返回数量相等的购物车单品
    for item in items.items():
        yield {
            'Name': item.find('div.item-msg div.p-name').text(),
            'Num': item.find('div.quantity-form input').attr['value'],
            'Price': item.find('p.plus-switch').text(),
            'Tips': item.find('div.props-txt').text(), #
        }



# 运行失败
# def writeFile(item):
#     with open('./result.csv', 'w', encoding='utf-8') as f:
#         writer = csv.DictWriter(f, fieldnames=['Name', 'Num', 'Price'])
#         writer.writeheader()  # 写入CSV文件的列名行，即告诉Python创建三列来存放数据
#         writer.writerow(item)  # 将包含字典的列表全部写入到CSV文件中
#         pass
#     pass


def main():
    """
    主函数，调度爬虫
    :param offset: 
    :return: 
    """
    url = 'https://cart.jd.com/cart.action'
    html = getPage(url)
    #print(html)  # 成功
    if html:
        # items = parsePage(html)
        # for item in items:
        #     print(item)  # 打印顺便检查进度
        #     writeFile(item)
        for item in parsePage(html):
            print(item)
            # writeFile(item)
    pass



if __name__ == '__main__':
    main()




